<?php

namespace App\System\Base\Repositories;

use App\Module\Api\Controllers\Request\TableRequest;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Model\BaseRegion;


class BaseRegionRepository extends Repository
{
    use SinglePattern;

    private $region;
    /**
     * @param object $request
     * @return mixed
     */
    public function getList(TableRequest $request){

        $data = BaseRegion::whereIn("deep",[1,2])
            ->orderBy($request->orderBy,$request->orderType)
            ->paginate($request->limit,['*'],'page',$request->page);
        foreach($data as $v) {
            $item = [];
            $item['id'] = $v->id;
            $item['parentId'] = $v->parent_id;
            $item['name'] = $v->name;
            $return['regions'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }

    /**
     *递归查询省市区信息
     */
    public function getOne($id)
    {
       $res =  BaseRegion::where('id',$id)->first();
       if($res && $res->parent_id)
       {
           $this->region[] = $res->toArray();
           return $this->getOne($res['parent_id']);
       }
       return $this->region;
    }
}